package com.wu.printminnumber;

import java.util.Arrays;

/**
 * 把数组排成最小的数
 *
 * @author lynn
 * @date 2020/8/14 - 15:39
 */
public class Solution {
    public static String PrintMinNumber(int[] numbers) {
        if (numbers == null || numbers.length == 0) {
            return "";
        }

        String[] dp = new String[numbers.length];
        Arrays.sort(numbers);
        dp[0] = String.valueOf(numbers[0]);
        for (int i = 1; i < numbers.length; i++) {
            String value1 = String.valueOf(numbers[i]).concat(String.valueOf(dp[i - 1]));
            String value2 = String.valueOf(dp[i - 1]).concat(String.valueOf(numbers[i]));
            dp[i] = value1.compareTo(value2) > 0 ? value2 : value1;
        }
        return dp[numbers.length - 1];
    }

    public static void main(String[] args) {
        int[] arr = new int[]{3334, 3, 3333332};
        String s = PrintMinNumber(arr);
        System.out.println(s);
    }
}
